//
//  NetworkTools.swift
//  sina
//
//  Created by DXapple on 15/11/19.
//  Copyright © 2015年 sqlite. All rights reserved.
//

import UIKit
import AFNetworking
private let  weiboDomain = "com.baidu.data.error"
private let hostname = "https://api.weibo.com/"

enum HTTPMethod: String {
    case POST = "POST"
    case GET = "GET"
    
}


class NetworkTools: AFHTTPSessionManager {
    static let sharedTools: NetworkTools = {
//        let urlstring = "https://api.weibo.com/"
        let url = NSURL(string: hostname)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    
    
    }()
    func requestJsonDict(method: HTTPMethod,urlString: String, parmaters: [String : AnyObject]?,finished:(result: [String : AnyObject]?, error: NSError?) ->()) {
        if  method == HTTPMethod.GET{
            
            
            GET(urlString, parameters: parmaters, success: { (_ , result) -> Void in
                
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                    //数据格式不匹配 自定义信息错误 定义errorcode为负值  domain Y域名的翻转
                }
                let dataErro = NSError(domain: weiboDomain, code: -1000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataErro)
                
                }) { (_, error ) -> Void in
                    finished(result: nil, error: error)
                    print(error)
                    
            }
            
        } else {
            POST(urlString, parameters: parmaters, success: { (_ , result) -> Void in
                
                if let  dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_ , error ) -> Void in
                    
                    finished(result: nil, error: error)
                    
                    
            })
            
            
        }
        
    }
    
    
    
}








    

